package com.weetop.cfw.mine.activity

import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.kongzue.dialog.v3.MessageDialog
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.weetop.cfw.R
import com.weetop.cfw.application.CFWApplication
import com.weetop.cfw.base.presenter.imp.WorkshopBrowseRecordPresenterImp
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.view.WorkshopBrowseRecordView
import com.weetop.cfw.bean.LeaseSaleBrowsingRecordBean
import com.weetop.cfw.bean.TestBean
import com.weetop.cfw.kind.activity.WorkshopDetailActivity
import com.weetop.cfw.mine.adapter.WorkshopBrowseRecordAdapter
import com.weetop.cfw.mine.adapter.WorkshopBrowseRecordOneAdapter
import com.weetop.cfw.other.WorkshopDetailWebViewActivity
import kotlinx.android.synthetic.main.activity_workshop_browse_record.*

/**
 * 厂房仓库租售浏览记录界面
 */
class WorkshopBrowseRecordActivity : CommonBaseActivity(),View.OnClickListener,WorkshopBrowseRecordView,OnRefreshLoadMoreListener {

    private lateinit var workshopBrowseRecordAdapter: WorkshopBrowseRecordOneAdapter

    private lateinit var workshopBrowseRecordPresenterImp: WorkshopBrowseRecordPresenterImp

    private var currentPageNumber = 1//当前数据的页数
    private var totalCount = 0//总数据量

    private var isRefresh = false//是否正在刷新
    private var isLoadMore = false//是否加载更多

    private val leaseSaleBrowsingRecordBeanList = ArrayList<LeaseSaleBrowsingRecordBean.DataBean>()

    override fun getLayoutId() = R.layout.activity_workshop_browse_record

    override fun initView(savedInstanceState: Bundle?) {
        workshopBrowseRecordPresenterImp = WorkshopBrowseRecordPresenterImp()
        workshopBrowseRecordPresenterImp.attachView(this)

       setViewsOnClickListener(this,imageWorkshopBrowseRecordBack,textClearRecord)
       smartRefreshLayout.setOnRefreshLoadMoreListener(this)
        smartRefreshLayout.setEnableAutoLoadMore(false)
    }

    override fun initData(savedInstanceState: Bundle?) {
        workshopBrowseRecordPresenterImp.getLeaseSaleBrowsingRecordData(this,currentPageNumber)

        workshopBrowseRecordAdapter = WorkshopBrowseRecordOneAdapter(R.layout.layout_item_workshop_browse_record,leaseSaleBrowsingRecordBeanList)
        workshopBrowseRecordAdapter.emptyView = emptyView
        workshopBrowseRecordAdapter.setOnItemClickListener { _, _, position ->
            val dataBean = leaseSaleBrowsingRecordBeanList[position]
            if(dataBean.intid == 1 && dataBean.typeid.contains("1201")){//优先跳转厂房出租
                WorkshopDetailActivity.startWorkshopDetailActivity(context,leaseSaleBrowsingRecordBeanList[position].infoid,2)
            }else if(dataBean.intid == 1 && dataBean.typeid.contains("1202")){//跳转仓库出租
                WorkshopDetailActivity.startWorkshopDetailActivity(context,leaseSaleBrowsingRecordBeanList[position].infoid,4)
            }else if(dataBean.intid == 2 && dataBean.typeid.contains("1201")){//跳转厂房出售
                WorkshopDetailActivity.startWorkshopDetailActivity(context,leaseSaleBrowsingRecordBeanList[position].infoid,1)
            }else if(dataBean.intid == 2 && dataBean.typeid.contains("1202")){//跳转仓库出售
                WorkshopDetailActivity.startWorkshopDetailActivity(context,leaseSaleBrowsingRecordBeanList[position].infoid,3)
            }
        }
        workshopBrowseRecordRV.adapter = workshopBrowseRecordAdapter
    }

    override fun onDestroy() {
        super.onDestroy()
        workshopBrowseRecordPresenterImp.detachView()
    }

    override fun onClick(p0: View?) {
       when(p0?.id){
           R.id.imageWorkshopBrowseRecordBack -> ActivityUtils.finishActivity(this)
           R.id.textClearRecord -> tipUserClearCloudCurriculumRecord()
       }
    }

    /**
     * 出租出售浏览记录清空成功
     */
    override fun workshopBrowseRecordClearSuccess() {
        smartRefreshLayout.autoRefresh()
    }

    /**
     * 提醒用户是否清空厂房仓库租售的浏览记录
     */
    private fun tipUserClearCloudCurriculumRecord(){
        MessageDialog.show(this,"提示","确定清空厂房仓库租售浏览记录？","清空","取消").setOnOkButtonClickListener { baseDialog, v ->
            workshopBrowseRecordPresenterImp.clearBrowsingRecordsOfFactoryBuildingsAndWarehouses(this@WorkshopBrowseRecordActivity)
            baseDialog.doDismiss()
            true
        }
    }

    /**
     * 获取出租出售浏览记录成功
     */
    override fun getLeaseSaleBrowsingRecordDataSuccess(leaseSaleBrowsingRecordBean: LeaseSaleBrowsingRecordBean?) {
        if(leaseSaleBrowsingRecordBean != null){

            totalCount = leaseSaleBrowsingRecordBean.records

            if(isRefresh){//处于刷新状态
                leaseSaleBrowsingRecordBeanList.clear()
                leaseSaleBrowsingRecordBeanList.addAll(leaseSaleBrowsingRecordBean.data)
                workshopBrowseRecordAdapter.notifyDataSetChanged()
                smartRefreshLayout.finishRefresh(true)
                if(leaseSaleBrowsingRecordBean.data.size > 0){
                    currentPageNumber = 1//将当前数据页数重置为1
                }
                isRefresh = false
                return
            }
            if(isLoadMore){
                leaseSaleBrowsingRecordBeanList.addAll(leaseSaleBrowsingRecordBean.data)
                workshopBrowseRecordAdapter.notifyDataSetChanged()
                smartRefreshLayout.finishLoadMore(true)
                if(leaseSaleBrowsingRecordBean.data.size > 0){
                    currentPageNumber += 1//将当前数据页数增加1
                }
                isLoadMore = false
                return
            }
            leaseSaleBrowsingRecordBeanList.addAll(leaseSaleBrowsingRecordBean.data)
            workshopBrowseRecordAdapter.notifyDataSetChanged()
        }
    }

    /**
     * 获取出租出售浏览记录失败
     */
    override fun getLeaseSaleBrowsingRecordDataFailed() {
        smartRefreshLayout.finishRefresh(false)
        smartRefreshLayout.finishLoadMore(false)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        if(totalCount <= leaseSaleBrowsingRecordBeanList.size){
            showNativeShortToast("暂无更多数据")
            smartRefreshLayout.finishLoadMoreWithNoMoreData()
        }else{
            isLoadMore = true
            workshopBrowseRecordPresenterImp.getLeaseSaleBrowsingRecordData(this,currentPageNumber + 1)
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        isRefresh = true
        workshopBrowseRecordPresenterImp.getLeaseSaleBrowsingRecordData(this,1)
    }

}
